import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    jwt:  localStorage.getItem('Authorization'),
    userInfo: sessionStorage.getItem('userInfo') ? JSON.parse(sessionStorage.getItem('userInfo')) : "" 
  },
  mutations: {
    SET_JWT(state, token) {
      state.jwt = token
      localStorage.setItem("Authorization",token)
    },
    SET_USERINFO(state, user) {
      state.userInfo = user
      sessionStorage.setItem("userInfo", JSON.stringify(user))
    },
    REMOVE_INFO(state) {
      state.userInfo = ""
      state.jwt = ""
      localStorage.setItem("Authorization","")
      sessionStorage.setItem("userInfo", "")
    }
  },
  getters: {
    getUser: state => {
        if(state.userInfo && state.userInfo != "") {
          return state.userInfo
        }
        return sessionStorage.getItem('userInfo') ? JSON.parse(sessionStorage.getItem('userInfo')) : "" 
    } 
  },
  actions: {

  },
  modules: {
  }
})
